// pages/douban/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        has_shown_splash: false,
        loading: true,
        // boards: [{key: 'in_theaters'}, {key: 'coming_soon'}, {key: 'top250'}],
        boards: [
          {
            title: "北京",
            movies: [
              {
                id: "0",
                mode: 'widthFix',
                images: {
                  // large: "https://pic.ntimg.cn/BannerPic/20211213/original/20211213090432_1.jpg",
                  large: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
                  text: "图片"
                }
              },
              {
                id: "1",
                mode: 'widthFix',
                images: {
                  large: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
                  text: "图片"
                }
              },
              {
                id: "2",
                mode: 'widthFix',
                images: {
                  large: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
                  text: "图片"
                }
              },
              {
                id: "3",
                mode: 'widthFix',
                images: {
                  large: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
                  text: "图片"
                }
              }
            ]
          },
          {
            title: "上海",
            movies: [
              {
                id: "0",
                mode: 'widthFix',
                images: {
                  // large: "https://pic.ntimg.cn/BannerPic/20211213/original/20211213090432_1.jpg",
                  large: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
                  text: "图片"
                }
              },
              {
                id: "1",
                mode: 'widthFix',
                images: {
                  large: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
                  text: "图片"
                }
              },
              {
                id: "2",
                mode: 'widthFix',
                images: {
                  large: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
                  text: "图片"
                }
              },
              {
                id: "3",
                mode: 'widthFix',
                images: {
                  large: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
                  text: "图片"
                }
              }
            ]
          },
          {
            title: "广州",
            movies: [
              {
                id: "0",
                mode: 'widthFix',
                images: {
                  // large: "https://pic.ntimg.cn/BannerPic/20211213/original/20211213090432_1.jpg",
                  large: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
                  text: "图片"
                }
              },
              {
                id: "1",
                mode: 'widthFix',
                images: {
                  large: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
                  text: "图片"
                }
              },
              {
                id: "2",
                mode: 'widthFix',
                images: {
                  large: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
                  text: "图片"
                }
              },
              {
                id: "3",
                mode: 'widthFix',
                images: {
                  large: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
                  text: "图片"
                }
              }
            ]
          }
        ],
        subjects: [{
            id: "0",
            mode: 'widthFix',
            images: {
              // large: "https://pic.ntimg.cn/BannerPic/20211213/original/20211213090432_1.jpg",
              large: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
              text: "图片"
            }
          },
          {
            id: "1",
            mode: 'widthFix',
            images: {
              large: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
              text: "图片"
            }
          },
          {
            id: "2",
            mode: 'widthFix',
            images: {
              large: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
              text: "图片"
            }
          },
          {
            id: "3",
            mode: 'widthFix',
            images: {
              large: "https://res.wx.qq.com/wxdoc/dist/assets/img/0.4cb08bb4.jpg",
              text: "图片"
            }
          }
        ],
        // 修改对应属性 需要使用this.setData({key: value})
        lazyLoad: true,
        indicatorDots: true,
        vertical: false,
        autoplay: false,
        interval: 2000,
        duration: 500
    },

    retrieveData() {
        let app = getApp();

        var promises = this.data.boards.map(board => {
            return app.request(`https://api.douban.com/v2/movie/${board.key}?start=0&count=10`).then(d => {
                if(!d) {
                    return board
                }
                board.title = d.title
                board.movies = d.subjects
                return board
            }).catch(err => {
                console.log(err)
            })
        })

        return app.promise.all(promises).then(boards => {
            if(!boards || !boards.length) {
                return
            }

            this.setData({ boards: boards, loading: false})
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        // 获取缓存 如果没有值就跳转去轮播页面
        wx.getStorage({
            key: "has_shown_splash",
            success(data) {
                // 请求数据
                // that.retrieveData()
            },
            fail: err => {
                wx.redirectTo({
                    url: '/pages/douban/splash'
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
      this.retrieveData().then(() => wx.stopPullDownRefresh())
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})